home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_29 / sounds.c < prev    next >
C/C++ Source or Header  |  1995-01-01  |  4KB  |  184 lines

  1. // SOUNDS.C - Demonstrates music and sound effects using PC speaker
  2.  
  3. // Written by Phil Inch for Game Developers Magazine (issue 2).
  4. // Contributed to the public domain.
  5.  
  6. // This program written and compiled with Borland C++ v3.1
  7. // Compatibility with other compilers is not guaranteed.
  8.  
  9. // Usage of this program is subject to the disclaimer printed
  10. // in the magazine.  You assume all risks associated with the use
  11. // of this program.
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <conio.h>
  21. #include <dos.h>
  22.  
  23. #define NOTE_DELAY 500
  24.  
  25. void connect_to_timer2( void ) {
  26.     asm {
  27.         in al,0x61
  28.         or al,3
  29.         out 0x61,al
  30.         }
  31. }
  32.  
  33. void disconnect_from_timer2( void ) {
  34.     asm {
  35.         in al,0x61
  36.         and al,252
  37.         out 0x61,al
  38.         }
  39. }
  40.  
  41. void set_timer2_countdown( unsigned int countdown ) {
  42.     char countlo, counthi;
  43.  
  44.     counthi = countdown/256;
  45.     countlo = countdown-counthi;
  46.  
  47.     asm {
  48.         mov dx,0x43
  49.         mov al,0xB6
  50.         out dx,al
  51.  
  52.         mov dx,0x42
  53.         mov al,countlo
  54.         out dx,al
  55.  
  56.         mov al,counthi
  57.         out dx,al
  58.         }
  59. }
  60.  
  61. void set_timer2_frequency( long freq ) {
  62.     unsigned int countdown;
  63.  
  64.     countdown = (unsigned int) (1193280L / freq);
  65.     set_timer2_countdown( countdown );
  66. }
  67.  
  68. void check_for_keystroke( void ) {
  69.     if ( kbhit() ) {
  70.         disconnect_from_timer2();
  71.         getch();
  72.         exit(0);
  73.         }
  74. }
  75.  
  76. void main( void ) {
  77.     int c;
  78.     long f;
  79.     clrscr();
  80.  
  81.     printf( "*** PC SPEAKER SOUND DEMO ***\n\n" );
  82.  
  83.     printf( "\n\nWARNING!  If you're running this at work, I suggest\n" );
  84.     printf( "you stop now, as this can be loud on some machines, and\n" );
  85.     printf( "the noises definitely do NOT sound like a Lotus spreadsheet!\n" );
  86.     printf( "\nTo stop now, press the ESCAPE key.  You can also stop the\n" );
  87.     printf( "demo at any time by pressing a key.  This will stop the demo\n" );
  88.     printf( "at the end of the current sound effect.\n" );
  89.  
  90.     printf( "\n... Press ESCAPE to abort, any other key to start ...\n\n" );
  91.     if ( getch() == 27 ) exit(0);
  92.  
  93.     printf( "The octave from C4 (middle C) to C5 ..." );
  94.  
  95.     connect_to_timer2();
  96.  
  97.     set_timer2_frequency( 261 );
  98.     check_for_keystroke();
  99.     delay(NOTE_DELAY);
  100.  
  101.     set_timer2_frequency( 293 );
  102.     check_for_keystroke();
  103.     delay(NOTE_DELAY);
  104.  
  105.     set_timer2_frequency( 329 );
  106.     check_for_keystroke();
  107.     delay(NOTE_DELAY);
  108.  
  109.     set_timer2_frequency( 349 );
  110.     check_for_keystroke();
  111.     delay(NOTE_DELAY);
  112.  
  113.     set_timer2_frequency( 392 );
  114.     check_for_keystroke();
  115.     delay(NOTE_DELAY);
  116.  
  117.     set_timer2_frequency( 440 );
  118.     check_for_keystroke();
  119.     delay(NOTE_DELAY);
  120.  
  121.     set_timer2_frequency( 494 );
  122.     check_for_keystroke();
  123.     delay(NOTE_DELAY);
  124.  
  125.     set_timer2_frequency( 523 );
  126.     check_for_keystroke();
  127.     delay(NOTE_DELAY);
  128.  
  129.     disconnect_from_timer2();
  130.  
  131.     delay(1000);
  132.  
  133.     printf( "\n\nAn alarm sound ..." );
  134.     connect_to_timer2();
  135.  
  136.     for ( c = 0; c < 5; c++ ) {
  137.         check_for_keystroke();
  138.         set_timer2_frequency( 349 );
  139.         delay(NOTE_DELAY);
  140.         set_timer2_frequency( 261 );
  141.         delay(NOTE_DELAY);
  142.         }
  143.     disconnect_from_timer2();
  144.  
  145.     delay(1000);
  146.  
  147.     printf( "\n\nA different alarm ..." );
  148.     connect_to_timer2();
  149.  
  150.     for ( c = 0; c < 5; c++ ) {
  151.         check_for_keystroke();
  152.         for ( f = 200; f <= 800; f+=10 ) {
  153.             set_timer2_frequency( f );
  154.             delay(10);
  155.             }
  156.         check_for_keystroke();
  157.         for ( f = 800; f >= 200; f-=10 ) {
  158.             set_timer2_frequency( f );
  159.             delay(10);
  160.             }
  161.         }
  162.     disconnect_from_timer2();
  163.  
  164.     delay(1000);
  165.  
  166.     printf( "\n\nA laser gun ..." );
  167.  
  168.     for ( c = 0; c < 5; c++ ) {
  169.     connect_to_timer2();
  170.         check_for_keystroke();
  171.         for ( f = 2700; f >= 2000; f-=10 ) {
  172.             set_timer2_frequency( f );
  173.             delay(6);
  174.             }
  175.         disconnect_from_timer2();
  176.         delay(400);
  177.         }
  178.  
  179.     printf( "\n\nThat's it - now you design some!\n\n" );
  180. }
  181.  
  182.  
  183.  
  184.